﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class EditMyProduct : System.Web.UI.Page
    {
        private void fillData()
        {
            int id = int.Parse(Request.QueryString["Id"]);
            NguoiDung nguoiDung = (NguoiDung)Session["CurrentUser"];
            if (SanPhamBUS.CheckProductOfUser(id, nguoiDung))
            {
                SanPham sanPham = SanPhamBUS.GetById(id);
                txtGiaSan.Text = sanPham.GiaSan.ToString();
                txtMoTaSanPham.Text = sanPham.MoTa;
                txtTenSanPham.Text = sanPham.TenSanPham;
                lbNgayDang.Text = String.Format("{0: hh:mm tt dd/MM/yyyy}", sanPham.NgayTao);
                ddlThoiGianDauGia.SelectedValue = sanPham.ThoiGianDauGia.ToString();
                ddlLoaiSanPham.DataSource = LoaiSanPhamBUS.GetAll();
                ddlLoaiSanPham.DataValueField = "Id";
                ddlLoaiSanPham.DataTextField = "TenLoaiSanPham";
                ddlLoaiSanPham.DataBind();
                ddlLoaiSanPham.SelectedValue = sanPham.LoaiSanPhamId.ToString();

                HinhDaiDien.ImageUrl = "images/" + sanPham.HinhAnh;
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            NguoiDung nguoiDung = (NguoiDung)Session["CurrentUser"];
            if (nguoiDung == null)
            {
                Response.Redirect("Default.aspx");
                return;
            }
            else if (nguoiDung.LoaiNguoiDungId == 2)
            {
                Response.Redirect("Default.aspx");
                return;
            }
            if (!IsPostBack)
            {
                fillData();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            SanPham sanPham = SanPhamBUS.GetById(int.Parse(Request.QueryString["id"]));
            sanPham.TenSanPham = txtTenSanPham.Text;
            sanPham.GiaSan = Utilities.Currency(txtGiaSan.Text);
            sanPham.MoTa = txtMoTaSanPham.Text;
            sanPham.ThoiGianDauGia = int.Parse(ddlThoiGianDauGia.SelectedValue);
            sanPham.LoaiSanPhamId = int.Parse(ddlLoaiSanPham.SelectedValue);
            //co cap nhat lai hình
            if (fuHinhDaiDien.FileName != "")
            {
                //xoa hinh cu
                File.Delete(Server.MapPath("~/images/") + sanPham.HinhAnh);
                //cap nhat hinh moi
                string hash = DateTime.Now.Ticks.ToString();
                fuHinhDaiDien.SaveAs(Server.MapPath("~/images/") + hash + "." + fuHinhDaiDien.FileName.Split('.')[1]);
                sanPham.HinhAnh = hash + "." + fuHinhDaiDien.FileName.Split('.')[1];
            }
            SanPhamBUS.Update(sanPham);
            Response.Redirect("MyProduct.aspx");
        }
    }
}